<?php
/*
商品详情
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$basic=$this->plugin_setting('basic');
$return['set']=$basic;
$return['setting']=$basic;
if($_RQ['op']=='main'){
	$id=$_RQ['id'];
	if(!$id){
		exi('商品已下架或被删除!','error');
	}
	$_SESSION['goodsid'] = $id;
	$goods=pdo_get('jy_creditshop_goods',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	//增加一次浏览次数
	pdo_update('jy_creditshop_goods', array('views' => $goods['views'] + 1), array('id' => $id));
	if(is_array($goods['dispatch'])){
		$isarray_dispatch=1;
	}else{
		$isarray_dispatch=0;
	}
	$goods['canbuy'] = !(empty($goods['status'])) && empty($goods['deleted'])?1:2;
	//exi($goods['canbuy'],'error');exit;
	
	if ($goods['canbuy']==2) {		
		$goods['buymsg'] = '已下架';
	}else{	
		if ($goods['goodstype'] == 3) {			
			if (($goods['packetsurplus'] <= 0) || ($goods['surplusmoney'] <= $goods['packetlimit'])) 
			{
				$goods['canbuy'] = 2;
				$goods['buymsg'] = ((empty($goods['type']) ? '已兑完' : '已抽完'));
			}
		}
		else if (0 < $goods['total']) 
		{
			$logcount = pdo_fetchcolumn('select count(*) from ' . tablename('jy_creditshop_log') . '  where goodsid=:goodsid and status>=2  and uniacid=:uniacid  ', array(':goodsid' => $id, ':uniacid' => $_SESSION['uniacid']));
			$goods['logcount'] = $logcount;
			if ($goods['joins'] < $logcount) 
			{
				pdo_update('jy_creditshop_goods', array('joins' => $logcount), array('id' => $id));
			}
		}
		else 
		{	
			$goods['canbuy'] = 2;
			$goods['buymsg'] = ((empty($goods['type']) ? '已兑完' : '已抽完'));
			
		}
	}
	//exi($goods['canbuy'],'error');exit;
	//规格
	$specs_list=pdo_getall('jy_creditshop_spec',array('goodsid'=>$id,'uniacid'=>$_SESSION['uniacid']),array('id','title'),'','displayorder asc');
	//已选规格
	$selectspecs = array();
	$selespename = '';
	if($specs_list){
			foreach($specs_list as $k=>&$sp){
				$sp['items']=pdo_getall('jy_creditshop_spec_item',array('specid'=>$sp['id'],'uniacid'=>$_SESSION['uniacid']),array('id','title'),'','displayorder asc');
				$selectspecs[$k]=$sp['items'][0]['id'];
				$selespename .=$sp['items'][0]['title'].'，';
			}
	}
	//默认已选参数：
	$spsids = '';
	foreach($selectspecs as $k=>&$s){
			if($k==0){
				$spsids .= $s;
			}else{
				$spsids .= '_'.$s;
			}
	}
	$return['options'] = pdo_get('jy_creditshop_option',array('goodsid'=>$id,'specs'=>$spsids));
	//默认商品规格价格：
	if($return['options']){
		$goods['money']=$return['options']['price'];
		$goods['credit']=$return['options']['credit'];
	}
	$return['selespename'] = $selespename;
	$option_list=pdo_getall('jy_creditshop_option',array('uniacid'=>$_SESSION['uniacid'],'goodsid'=>$id),array('id','specs','thumb','total','price','credit','goodssn','productsn','weight'),'','displayorder asc');
	if($option_list){
			foreach($option_list as &$i){
				$i['specs']=explode('_',$i['specs']);
				foreach($i['specs'] as $j){
					$op=pdo_get('jy_creditshop_spec_item',array('id'=>intval($j)),array('specid','title'));
					$spec_title=pdo_getcolumn('jy_creditshop_spec',array('id'=>$op['specid']),'title');
					$i[$spec_title]=$op['title'];
				}
				$i['total']=$i['total'];
				$i['credit']=$i['credit'];
				$i['goodssn']=$i['goodssn'];
				$i['productsn']=$i['productsn'];
				$i['thumb']=$i['thumb'];
			}
			$specs_list['option']=$option_list;
	}
	$return['specs_list'] = $specs_list;
	//评价
	$parameter=array(
			'params'=>array('goodsid'=>$id,'checked'=>1,'uniacid'=>$_SESSION['uniacid']),
			'fields'=>'',
			'key'=>'',
			'orderby'=>'time desc',
			'limit'=>'50',
	);
	$total=0;
	$comment = pdo_getlist('jy_creditshop_comment',$parameter,$total);
	if($comment){
		foreach($comment as &$cv){
			$cv['member']=cfc('core')->member_show($cv['mid']);
		}
	}
	$pingjianums=pdo_count('jy_creditshop_comment',$parameter['params'],'id');	
	if($pingjianums>0 && $basic['no_pingjianum']!=1){
		$pingjianums=mt_rand(1000,10000);
	}
	//参与记录
	$parameter=array(
			'params'=>array('goodsid'=>$id,'uniacid'=>$_SESSION['uniacid']),
			'fields'=>'',
			'key'=>'',
			'orderby'=>'createtime desc',
			'limit'=>'',
	);
	$total=0;
	$log = pdo_getlist('jy_creditshop_log',$parameter,$total);
	$logtotal=0;
	if($log){
		//参与记录次数
		$logtotal=count($log);
		foreach($log as &$item){
			$member=pdo_get('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['mid']));
			$item['avatar'] = $member['avatar'];
			$item['nickname'] = $member['nickname'];
			$item['createtime_str'] = date('Y/m/d H:i', $item['createtime']);
		}
	}
	//推荐商品
	$parameter=array(
			'table'=>'jy_creditshop_goods',
			'params'=>array('status'=>1,'uniacid'=>$_SESSION['uniacid'],'isrecommand'=>'1'),
			'fields'=>'',
			'key'=>'',
			'orderby'=>'displayorder asc',
			'limit'=>'3',
	);
	$total=0;
	$pindex = 1;
	$psize = 10;
	$goodsrec=pdo_getslice('jy_creditshop_goods',$params['params'],array($pindex,$psize),$total,$params['fields'],'','');
	$goodsrec=array($goodsrec['0'],$goodsrec['1'],$goodsrec['2']);
	set_html('title',$basic['title']?'商品详情-'.$basic['title']:'商品详情');
	$return['sharedata']=array(
		'title'=>$basic['share_title'],
		'desc'=>$basic['share_desc'],
		'link'=>custom_url('jy_creditshop/goods_detail/',true),
		'imgUrl'=>ATTACHMENT_URL.$basic['share_image'],
	);
	//POST提交：
	$post = array();
	$post['num'] = 1;
	$post['goodsid'] = $id;
	$post['specid']=$selectspecs;
	$post['optionid']=$return['options']['id'];
	$return['post']=$post;
	$end_time=strtotime(date('Y-m-d',strtotime('+1 day')));
	$end_time=$end_time-TIMESTAMP;
	$return['rest_time']=model('jy_weishop/model')->date($end_time);
	$return['comment'] = $comment;
	$return['pingjianums'] = $pingjianums;
	$return['log'] = $log;
	$return['logtotal'] = $logtotal;
	$return['goodsrec'] = $goodsrec;
	if($goods['images']){
		$goods['images']=json_decode($goods['images'],true);
	}else{
		$goods['images'][]=$goods['thumb'];
	}
	$goods['salenum']=price($goods['salenum'],'',true);
	$return['goods'] = $goods;
	//获取行政数据
	$return['province']=cfc('tools')->pcd();
	exi($return);
}
if($_RQ['op']=='exchange'){
	$post=get_postdata();
	//print_r($post);exit;
	$id=intval($post['goodsid']);
	$number=intval($post['num']);
	if(!$id){
		exi('商品不存在或已删除！','error');
	}
	if(!$number){
		$number=1;
	}
	$spsids = '';
	foreach($post['specid'] as $k=>&$s){
			if($k==0){
				$spsids .= $s;
			}else{
				$spsids .= '_'.$s;
			}
	}
}
if($_RQ['op']=='get_optionsid'){
	$post=get_postdata();
	$id=$_SESSION['goodsid'];
	if(!$id){
		exi('请先选择商品！','error');
	}
	
	//规格
	$specids=explode('_',$post['specid']);
	
	//已选规格
	$selespename = '';
	if($specids){
			foreach($specids as $k=>$sp){
				$spec_item=pdo_get('jy_creditshop_spec_item',array('id'=>$sp,'uniacid'=>$_SESSION['uniacid']),array('id','title'));
				$selespename .=$spec_item['title'].'，';
			}
	}
	$return['options'] = pdo_get('jy_creditshop_option',array('goodsid'=>$id,'specs'=>$post['specid']));
	$return['selespename'] = $selespename;
	exi($return);
}
if($_RQ['op']=='pay'){
	$post=get_postdata();
	if($post['addressid']){
		$address=pdo_get('core_address',array('uniacid'=>$_SESSION['uniacid'],'id'=>$post['addressid']));
	}
	$id=intval($post['goodsid']);
	if(!$id){
		exi('未获取到商品信息！','error');
	}
	$num=intval($post['num'])?:1;
	$optionid=intval($post['optionid']);
	$goods=pdo_get('jy_creditshop_goods',array('id'=>$id));
	if($optionid){
			$options = pdo_get('jy_creditshop_option',array('id'=>$optionid));
			$goods['optionid']=$options['id'];
			$goods['money']=$options['price'];
			$goods['credit']=$options['credit'];
	}else{
		$goods['money']=$goods['price'];
	}
	$money=$goods['money']*$num;
	$credit=$goods['credit']*$num;
	if($goods['goodstype']==0 && !$goods['isverify']){
		$dispatchprice=intval($post['dispatchprice']);
		$money=$money+$dispatchprice;
		if(!$post['addressid'] && !$post['address']){
			exi('请填写收货地址！','error');
		}
	}	
	//判断用户的积分是否够用
	if(!$_SESSION['uid']){
		$_SESSION['nowcredit1']=$_SESSION['issign']?$return['pluginset']['jifen']+$return['pluginset']['day1']:$return['pluginset']['jifen'];
		if($_SESSION['nowcredit1']<$credit){
			exi('您的积分不足，提交失败！','error');
		}
	}else{
		$user_credit=pdo_getcolumn('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_SESSION['uid']),'credit1');
		if($user_credit<$credit){
			exi('您的积分不足，提交失败！','error');
		}	
	}
	$_SESSION['mobile']=$post['mobile'];
	$post['address']=array('address'=>$post['address'],'pcd'=>ijson_encode($post['pcd']));
	//兑换商品
	$insert=array(
		'uniacid'=>$_SESSION['uniacid'],
		'merchid'=>intval($goods['merchid']),
		'mid'=>$_SESSION['uid'],
		'goodsid' => $id,
		'logno' =>get_ordersn('jy_creditshop_log','logno'),
		'type' => $goods['type'],
		'money' => $money,
		'credit' => $credit,
		'storeid' => $post['storeid'],
		'optionid' => $goods['optionid'],
		'addressid' => $post['addressid'],
		'dispatch' => $goods['dispatch'],
		'address' => $post['addressid']?'':ijson_encode($post['address']),
		'status' => 0,
		'paystatus' => 0,
		'dispatchstatus' =>0,
		'createtime' => time(),
		'realname' => trim($address['name'])?:$post['name'],
		'mobile' => trim($address['mobile'])?:$post['mobile'],
	);
	   $order_id=model('jy_creditshop/order')->create($insert);
	   //支付
	   $params=array(
			'id'=>$order_id,
			'title'=>'积分优品',
			'fee'=>$money,
			//'pay_type'=>'wechat',//可选，此项作用为指定支付方式
			'no_type'=>array('hdfk'),//可选，此项作用为指定不允许使用哪种支付方式
			'back_url'=>'jy_jifenyou/goods_detail/payresult/',//必填，指定异步支付结果通知地址，采用系统标准URL书写格式
			'success_url'=>'jy_jifenyou/order/',
			'error_url'=>'jy_jifenyou/order/',
			'plugin'=>'jy_jifenyou',
		);
		$payid=cfc('pay')->create_pay_order($params);
		pdo_update('jy_creditshop_log',array('pay_id'=>$payid), array('id' => $order_id));		
		$res=array(
			'pay_id'=>$payid,
			'order_id'=>$order_id,
			'success_url'=>$params['success_url'],
			'error_url'=>$params['error_url'],
		);
		exi($res);
}
if($_RQ['op']=='payresult'){
	$payresult=$_SESSION['pay_result'];
	if(!$payresult){
		exi('支付失败，未收到支付结果！','error');
	}
	if($payresult['status']!=1){
		exi('支付失败！','error');
	}
	$DD=ijson_encode($payresult);
	$order=pdo_get('jy_creditshop_log',array('id'=>$payresult['id']));
	if(!$order){
		exi('支付失败！订单不存在！','error');
	}
	$params=array(
		'paytype'=>$payresult['pay_type'],
		'transid'=>$payresult['transaction_id'],
	);
	model('jy_creditshop/order')->order_pay($order['id'],$params);
	exi('提交成功!');
}